const cache: {
  [props: string]: string[];
} = {};

export const composeChar = (str: string) => {
  const arr = str.split('');
  const len = arr.length;
  let result: string[] = [];

  if (cache[str]) {
    return cache[str];
  }
  
  if (len > 1) {
    for (let i = 0; i < len; i++){
      const currentChar = arr[i];
      const children = composeChar(arr.filter((...rest) => rest[1] !== i).join(''));
      
      children.forEach(item => {
        result.push(`${currentChar}${item}`);
      });
    }
  } else {
    result = [str];
  }

  cache[str] = result;
  return result;
};
